<?php

namespace Roles\Controller;

use Zend\Mvc\Controller\AbstractActionController,
    Zend\View\Model\ViewModel;

use Roles\Model\UsuarioXPerfil;
use Roles\Form\UserSearchForm;

class UsuarioXPerfilController extends AbstractActionController {

   protected $usuarioTable;
   protected $perfilTable;
   protected $usuarioXperfilTable;

   private function checkLogin() {
      if (! $this->getServiceLocator()
                 ->get('AuthService')->hasIdentity()){
         return $this->redirect()->toRoute('login');
      }
   }
   private function checkRole() {
      $username = $this->getServiceLocator()->get('AuthService')->getIdentity();
      $usuario = $this->getUsuarioTable()->getUsuarioByUserName($username);

      $path = $this->getRequest()->getUri()->getPath();
      if ($path[0] != '/') $path = '/' . $path;
      $pieces = explode("/",$path);
      $module = $pieces[1]; $action = $pieces[2];
      if ($action == "") $action = "index";

      if (! $this->getServiceLocator()
                 ->get('RoleService')->hasPermission($usuario->id,$module,$action)) {

         return $this->redirect()->toRoute('restringido');
      }
   }

   public function indexAction() {

      $this->checkLogin();
      $this->checkRole();

      $form = new UserSearchForm();
      $form->get('submit')->setValue('Buscar');

      $usuarios = array();
      $request = $this->getRequest();
      if ($request->isPost()) {
         $data = $request->getPost();
         if (strlen($data['nombre']) == 0 && strlen($data['apellido']) == 0)
            $usuarios = $this->getUsuarioTable()->fetchAll();
         else
            $usuarios = $this->getUsuarioTable()->searchUsersByName($data['nombre'],$data['apellido']);
      }

/*      return new ViewModel(array(
         'usuarios' => $this->getUsuarioTable()->fetchAll(),
      )); */

      return array(
         'form' => $form,
         'usuarios' => $usuarios,
      );

   }

   public function asociarAction() {

      $this->checkLogin();

      $request = $this->getRequest();

      $id = (int) $this->params()->fromRoute('id', 0);
      if (!$id && !$request->isPost()) {
         return $this->redirect()->toRoute('us_pf');
      }

      $form = new UserSearchForm();
      $form->get('submit')->setValue('Guardar');

      if ($request->isPost()) {

         $data = $request->getPost();
         $id = $data['usuario_id'];

         $perfiles = $this->getPerfilTable()->getPerfilesByUsuario($id);
         $i = 0;
         foreach ($perfiles as $perfil) {
            if ($data['checkbox_' . $i] == "on" && is_null($perfil->id_usuario_perfil)) {
                $usuarioXperfil = new UsuarioXPerfil();
                $usuarioXperfil->setIdUsuario($data['usuario_id']);
                $usuarioXperfil->setIdPerfil($perfil->id);
                $this->getUsuarioXPerfilTable()->saveUsuarioXPerfil($usuarioXperfil);
            } else if ($data['checkbox_' . $i] == "off" && !is_null($perfil->id_usuario_perfil)) {
                $this->getUsuarioXPerfilTable()->deleteUsuarioXPerfil($perfil->id_usuario_perfil);
            }

            $i++;
         }

         $form->get('submit')->setAttribute("disabled", true);

      }

      $usuario = $this->getUsuarioTable()->getUsuario($id);
      $perfiles = $this->getPerfilTable()->getPerfilesByUsuario($id);

      return array(
          'form'     => $form,
          'usuario'  => $usuario,
          'perfiles' => $perfiles,
      );

   }

   public function getUsuarioTable() {
      if (!$this->usuarioTable) {
         $sm = $this->getServiceLocator();
         $this->usuarioTable = $sm->get('Usuarios\Model\UsuarioTable');
      }
      return $this->usuarioTable;
   }

   public function getPerfilTable() {
      if (!$this->perfilTable) {
         $sm = $this->getServiceLocator();
         $this->perfilTable = $sm->get('Roles\Model\PerfilTable');
      }
      return $this->perfilTable;
   }

   public function getUsuarioXPerfilTable() {
      if (!$this->usuarioXperfilTable) {
         $sm = $this->getServiceLocator();
         $this->usuarioXperfilTable = $sm->get('Roles\Model\UsuarioXPerfilTable');
      }
      return $this->usuarioXperfilTable;
   }

}
